//
//  BiGuideView.m
//  GottaWash
//
//  Created by bindu on 14-9-8.
//  Copyright (c) 2014年 QiHe. All rights reserved.
//

#import "BiGuideView.h"

@interface BiGuideView()<UIScrollViewDelegate>
{
    NSMutableArray * _dotViewArray;
}
@end

@implementation BiGuideView

#define IPHONE4Array  [NSArray arrayWithObjects:@"guide/guide1_480.png", @"guide/guide2_480.png", @"guide/guide3_480.png", @"guide/guide4_480.png", nil];
#define IPHONE5Array  [NSArray arrayWithObjects:@"guide/guide1_568.png", @"guide/guide2_568.png", @"guide/guide3_568.png", @"guide/guide4_568.png", nil];

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor whiteColor];
        
        _dotViewArray = [[NSMutableArray alloc] init];
        
        _scrollView = [[UIScrollView alloc] initWithFrame:frame];
        _scrollView.pagingEnabled = YES;
        [_scrollView setShowsHorizontalScrollIndicator:NO];
        [self addSubview:_scrollView];
        [self fillGuide];
    }
    return self;
}

- (void)dealloc
{
    [_scrollView release];
    [_dotViewArray release];
    
    [super dealloc];
}

- (void)fillGuide
{
    _scrollView.contentSize = CGSizeMake(320*4, self.frame.size.height);
    _scrollView.delegate = self;
    
    NSArray * imageNameArray = IPHONE4Array;
    
    if(isIPhone5)
    {
        imageNameArray = IPHONE5Array;
    }
    
    UIImage * guide1Image = LOAD_IMAGE_WITH_CACHE([imageNameArray objectAtIndex:0]);
    UIImage * guide2Image = LOAD_IMAGE_WITH_CACHE([imageNameArray objectAtIndex:1]);
    UIImage * guide3Image = LOAD_IMAGE_WITH_CACHE([imageNameArray objectAtIndex:2]);
    UIImage * guide4Image = LOAD_IMAGE_WITH_CACHE([imageNameArray objectAtIndex:3]);
    
    UIView * guide1View = [self getCurGuideView:guide1Image withPage:1];
    UIView * guide2View = [self getCurGuideView:guide2Image withPage:2];
    UIView * guide3View = [self getCurGuideView:guide3Image withPage:3];
    UIView * guide4View = [self getCurGuideView:guide4Image withPage:4];
    
    [_scrollView addSubview:guide1View];
    [_scrollView addSubview:guide2View];
    [_scrollView addSubview:guide3View];
    [_scrollView addSubview:guide4View];
    
    int dotY = 450;
    if (isIPhone5)
    {
        dotY = 501;
    }
    
    for (int i = 0; i < 4; i++)
    {
        UIView * dotView = [[UIView alloc] initWithFrame:CGRectMake(128+i*(10+9), dotY, 10, 10)];
        dotView.backgroundColor = UIColorFromRGB(0x94d0ff);
        dotView.layer.cornerRadius = 4;
        dotView.clipsToBounds = YES;
        if (0 == i)
        {
            dotView.backgroundColor = UIColorFromRGB(0x0090ff);
        }
        [self addSubview:dotView];
        [_dotViewArray addObject:dotView];
        [dotView release];
    }
}

- (void)refreshDotView
{
    int index = _scrollView.contentOffset.x/_scrollView.frame.size.width;
    
    for (int i = 0; i < _dotViewArray.count; i++)
    {
        UIView * dotView = [_dotViewArray objectAtIndex:i];
        if (index == i)
        {
            dotView.backgroundColor = UIColorFromRGB(0x0090ff);
        }
        else
        {
            dotView.backgroundColor = UIColorFromRGB(0x94d0ff);
        }
    }
}

- (UIView *)getCurGuideView:(UIImage*)guideImage withPage:(int)page
{
    UIImageView * guideView = [[UIImageView alloc] initWithFrame:CGRectMake(320*(page-1), 0, guideImage.size.width, guideImage.size.height)];
    guideView.image = guideImage;
    guideView.userInteractionEnabled = YES;
    
    if (4 == page)
    {
        int margin = 120;
        int btnY = 395;
        if (isIPhone5)
        {
            btnY = 440;
            margin = 200;
        }
        
        UIButton * enterBtn = [[UIButton alloc] initWithFrame:CGRectMake((MAIN_SCREEN_WIDTH-195)/2, btnY, 195, 39)];
        [enterBtn setBackgroundImage:LOAD_IMAGE_WITH_CACHE(@"guide/btn.png") forState:UIControlStateNormal];
        [enterBtn addTarget:self action:@selector(onEnterBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [guideView addSubview:enterBtn];
        [enterBtn release];
    }
    
    return [guideView autorelease];
}

- (void)onEnterBtnClick:(id)sender
{
    [self removeFromSuperview];
}

#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self refreshDotView];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self refreshDotView];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self refreshDotView];
}

@end
